package com.xuxin.baby.page;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.xuxin.baby.R;
import com.xuxin.baby.activity.SearchActivity;
import com.xuxin.baby.activity.VideoActivity;
import com.xuxin.baby.adapter.TeachGroupAdapter;
import com.xuxin.baby.adapter.TeachVideoAdapter;
import com.xuxin.baby.base.BaseConstant;
import com.xuxin.baby.base.BaseFragment;
import com.xuxin.baby.bean.CategoryBean;
import com.xuxin.baby.bean.ReaderVideoBean;
import com.xuxin.baby.contract.CategoryContract;
import com.xuxin.baby.contract.TeachContract;
import com.xuxin.baby.fragment.teach.TeachCourseFragment;
import com.xuxin.baby.presenter.CategoryPresenter;
import com.xuxin.baby.presenter.TeachPresenter;
import com.xuxin.baby.utils.ACache;
import com.xuxin.baby.utils.RecycleUtils;
import com.xuxin.smartTabLayout.CommonTabLayout;
import com.xuxin.smartTabLayout.listener.CustomTabEntity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 精婴早教
 */
public class TeachFragment extends BaseFragment implements TeachContract.View, CategoryContract.View {
    private final TeachContract.Presenter presenter = new TeachPresenter(this);
    private final CategoryContract.Presenter presenter1 = new CategoryPresenter(this);
    private final ArrayList<CustomTabEntity> mEntity = new ArrayList<>();
    private final ArrayList<Fragment> mFragments = new ArrayList<>();
    @BindView(R.id.teach_refresh)
    SmartRefreshLayout teach_refresh;
    @BindView(R.id.fragment_teach_advise)
    LinearLayout fragment_teach_advise;
    @BindView(R.id.teach_recycle_video)
    RecyclerView teach_recycle_video;
    @BindView(R.id.teach_group_recycle)
    RecyclerView teach_group_recycle;//课程分类
    @BindView(R.id.fragment_teach_group)
    LinearLayout fragment_teach_group;
    @BindView(R.id.teach_tab)
    CommonTabLayout teach_tab;
    @BindView(R.id.teach_fragment)
    FrameLayout teach_fragment;

    private TeachVideoAdapter myAdapter;//推荐视频
    private TeachGroupAdapter myAdapter1;//课程分类

    private String tenantId;//租户id
    private String token;//授权token

    public static TeachFragment newInstance() {
        return new TeachFragment();
    }

    @Override
    public View setContentView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
        return inflater.inflate(R.layout.fragment_teach, container, false);
    }

    @Override
    public void initView() {
        /**
         * 智能刷新
         */
        teach_refresh.setEnableLoadMore(false);
        teach_refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                handler.sendEmptyMessage(0);
            }
        });
        //推荐视频
        myAdapter = new TeachVideoAdapter();
        RecycleUtils.horizontal(teach_recycle_video);
        teach_recycle_video.setAdapter(myAdapter);
        myAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString(BaseConstant.videoId, myAdapter.getData().get(position).getId());
                ActivityUtils.startActivity(bundle, VideoActivity.class);
            }
        });
        //课程分类
        RecycleUtils.init(teach_group_recycle);
        myAdapter1 = new TeachGroupAdapter();
        teach_group_recycle.setAdapter(myAdapter1);
        myAdapter1.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                myAdapter1.setPosition(position);
                if (teach_tab.getTabCount() > position) {
                    teach_tab.setCurrentTab(position);
                }
            }
        });
    }

    @Override
    public void initData() {
        ACache aCache = ACache.get(getContext());
        tenantId = aCache.getAsString(BaseConstant.tenantId);
        token = aCache.getAsString(BaseConstant.token);
        myAdapter.setList(null);
        handler.sendEmptyMessage(0);
    }

    @Override
    public void onComplete() {
        dismiss();
        teach_refresh.finishRefresh();
        teach_refresh.finishLoadMore();
    }

    @Override
    @OnClick({R.id.search_main})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.search_main) {
            ActivityUtils.startActivity(SearchActivity.class);
        }
    }

    private final Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case 0://精婴早教推荐视频
                    presenter.getVideo(tenantId, token, BaseConstant.categoryId_main);
                    break;
                case 1://课程分类
                    presenter1.getCategory(tenantId, token, "course", BaseConstant.categoryId_teach);
                    break;
            }
            return false;
        }
    });

    @Override
    public void onSuccessVideo(ReaderVideoBean readerVideoBean) {
        myAdapter.setList(readerVideoBean.getResult().getItems());
        if (myAdapter.getData().size() > 0) {
            fragment_teach_advise.setVisibility(View.VISIBLE);
        }
        handler.sendEmptyMessage(1);
    }

    @Override
    public void onSuccess(CategoryBean categoryBean) {
        fragment_teach_group.setVisibility(View.VISIBLE);
        if (null != categoryBean.getResult() &&
                null != categoryBean.getResult() &&
                categoryBean.getResult().size() > 0) {

            if (teach_tab.getTabCount() == 0) {
                try {
                    myAdapter1.setList(categoryBean.getResult());
                    for (int i = 0; i < categoryBean.getResult().size(); i++) {
                        mFragments.add(TeachCourseFragment.newInstance(categoryBean.getResult().get(i).getCategoryName(), categoryBean.getResult().get(i).getId()));
                        mEntity.add(new MainEntity(categoryBean.getResult().get(i).getCategoryName(), R.mipmap.ic_launcher, R.mipmap.ic_launcher));
                    }
                    teach_tab.setTabData(mEntity, (FragmentActivity) context, R.id.teach_fragment, mFragments);
                    teach_tab.setCurrentTab(0);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                LogUtils.e("count = " + teach_tab.getTabCount());
            }

        }
    }

    public static class MainEntity implements CustomTabEntity {
        private final String title;
        private final int selected_id;
        private final int unselected_id;

        private MainEntity(String title, int selected, int unselected) {
            this.title = title;
            this.selected_id = selected;
            this.unselected_id = unselected;
        }

        @Override
        public String getTabTitle() {
            return title;
        }

        @Override
        public int getTabSelectedIcon() {
            return selected_id;
        }

        @Override
        public int getTabUnselectedIcon() {
            return unselected_id;
        }
    }
}